package com.example.bo.test4;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends AppCompatActivity {
    ImageView img;
    Bitmap bitmap;
    Button downbtn;
    DownUtil downUtil;
    EditText textpath;
    String path;
    ProgressBar progress;
    private int percent;
   android.os.Handler handler=new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 123) {
                img.setImageBitmap(bitmap);
            } else if (msg.what == 124) {
                progress.setProgress(percent);
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
      //  img=(ImageView)findViewById(R.id.imagedow);
        textpath=(EditText)findViewById(R.id.path);
        downbtn=(Button)findViewById(R.id.downbtn);
        progress=(ProgressBar)findViewById(R.id.downprogress);
        downbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                path = textpath.getText().toString();
                downUtil = new DownUtil(path, "/sdcard/tdbtest/4.jpg", 2);
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            downUtil.download();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        
                        final Timer timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                percent = (int) (downUtil.getcompleteRate() * 100);
                                handler.sendEmptyMessage(124);
                                if (percent >= 100) {
                                    timer.cancel();
                                }
                            }
                        }, 0, 1000);
                    }
                }.start();

        /*new Thread(){
            public void run(){
                try {



                    URL url = new URL("http://imgsrc.baidu.com/forum/w%3D580/sign=47fc641c9358d109c4e3a9bae159ccd0/313c728b4710b9125254f19cc2fdfc03934522ec.jpg");
                    InputStream is=url.openStream();
                    bitmap = BitmapFactory.decodeStream(is);
                    handler.sendEmptyMessage(123);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }.start();*/

            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
